'use strict';

/**
 * @name CommentSearchCtrl
 * @description 查看所有备注
 * @author ZYF
 */

angular.module('metalabOperationApp').controller('CommentSearchCtrl', ['$scope', 'params', 'MetaResources', 'MetaRemind', '$uibModalInstance', 'MetaUtils', 'MetaSearch', function($scope, params, MetaResources, MetaRemind, $uibModalInstance, MetaUtils, MetaSearch) {
    $scope.listCtrl = {
        searchParams: {},
        tableParams: {}
    };
    $scope.init = function() {
        //初始化表格参数
        MetaUtils.initTableModule($scope.listCtrl.tableParams, null, $scope.changeUrl, true);
        //其他初始化
        $scope.listCtrl.tableParams.currentPage = 1;
        $scope.listCtrl.searchParams.search = function(event) {
            if (event.keyCode === 13) {
                $scope.changeUrl();
            }
        };
    };
    $scope.getListData = function(page) {
        var queryParams = {
            'type': 'comment',
            'searchText': $scope.listCtrl.searchParams.value,
            '_embed': 'user'
        };
        MetaResources.search.search(
            queryParams,
            function(data, header) {
                //先获取总数
                $scope.listCtrl.tableParams.total = parseInt(header()['x-count']);
                if (isNaN($scope.listCtrl.tableParams.total)) {
                  $scope.listCtrl.tableParams.total = 0;
                  $scope.listCtrl.tableParams.data = [];
                  $scope.listCtrl.tableParams.currentPage = 1;
                } else {
                  queryParams._range = MetaUtils.getRange(page, $scope.listCtrl.tableParams.pageNumber);
                  //获取数据
                  MetaResources.search.search(
                      queryParams,
                      function(data, header) {
                          $scope.listCtrl.tableParams.data = data;
                          $scope.listCtrl.tableParams.currentPage = page;
                          for (var i = 0; i < $scope.listCtrl.tableParams.data.length; i++) {
                              if ($scope.listCtrl.tableParams.data[i].targetType) {
                                  switch ($scope.listCtrl.tableParams.data[i].targetType) {
                                      case 'Demand':
                                          $scope.listCtrl.tableParams.data[i].type = "需求／合作";
                                          $scope.isCooperation('demand', $scope.listCtrl.tableParams.data[i].targetId, $scope.listCtrl.tableParams.data[i]);
                                          break;
                                      case 'Technology':
                                          $scope.listCtrl.tableParams.data[i].type = "技术／解决";
                                          $scope.isCooperation('technology', $scope.listCtrl.tableParams.data[i].targetId, $scope.listCtrl.tableParams.data[i]);
                                          break;
                                      case 'Talent':
                                          $scope.listCtrl.tableParams.data[i].type = "人才";
                                          break;
                                      case 'User':
                                          $scope.listCtrl.tableParams.data[i].type = "用户";
                                          break;
                                      case 'SolutionProcess':
                                          $scope.listCtrl.tableParams.data[i].type = "进程";
                                          break;
                                      default:
                                          break;
                                  }
                              } else {
                                  $scope.listCtrl.tableParams.data[i].type = "无类型";
                              }
                          }
                      },
                      function(error) {
                          MetaRemind.serverError(error);
                      });
                }
            },
            function(error) {
                MetaRemind.serverError(error);
            }
        );
    };
    $scope.isCooperation = function(type, id, comment) {
      MetaResources[type].get({'id': id},
          function(data) {
              if (data.tags[0]==="合作") {
                  comment.isCoop = true;
              } else {
                  comment.isCoop = false;
              }
          },
          function(error) {
              MetaRemind.serverError(error);
          });
    };
    $scope.checkComment = function() {
      $state.go

    };
    $scope.cancel = function() {
        $uibModalInstance.dismiss('cancel');
    };
    $scope.changeUrl = function() {
        $scope.getListData($scope.listCtrl.tableParams.currentPage);
    };
    $scope.init();
}]);
